import os
import pandas as pd
from flask import request, send_from_directory
from dash import get_app
from dash import (
    dcc,
    html,
    callback,
    Output,
    Input,
    State,
    register_page,
)
from ddbtools import (
    BaseCRUD,
    DBDf,
    Filter,
    Comparator,
    get_table_info,
    get_table_columns,
)
from gypb.db import get_db
import feffery_antd_components as fac
from gypb.component import CRUDTable

app = get_app()
register_page(__name__, name="代销项目概览", order=0)


project_overview = CRUDTable(
    name="代销项目表",
    db_path="dfs://infos",
    table_name="sale_projects",
    key_cols=["project_id"],
    # db_cols=[
    #     "project_name",
    #     "manager_code",
    #     "manager_name",
    #     "issue_dept",
    #     "issue_dept_personnel",
    #     "personnel",
    #     "sale_range",
    #     "prod_code",
    #     "prod_name",
    #     "strategy",
    #     "benchmark",
    #     "sale_date",
    #     "unsale_date",
    #     "risk_level",
    #     "open_date",
    #     "status",
    #     "create_date",
    # ],
    # col_names=[
    #     "项目名称",
    #     "管理人代码",
    #     "管理人名称",
    #     "发起部门",
    #     "发起部门对接人",
    #     "项目受理人",
    #     "销售范围",
    #     "产品代码",
    #     "产品名称",
    #     "策略类型",
    #     "业绩基准",
    #     "上架日期",
    #     "下架日期",
    #     "风险等级",
    #     "开放期",
    #     "状态",
    #     "项目创建日期",]
    editable_cols=[
        "项目名称",
        "管理人备案编码",
        "管理人名称",
        "发起部门",
        "项目对接人",
        "项目经理",
        "代销范围",
        "产品备案编码",
        "产品名称",
        "投资策略",
        "业绩基准",
        "代销上线日期",
        "代销下架日期",
        "风险等级",
        "开放日安排",
        "项目状态",
        "项目创建日期",
    ]
)


# def get_sales_project_table():
#     with get_db() as session:
#         db = "dfs://infos"
#         table = "sale_projects"
#         crud = BaseCRUD(db, table)
#         project_data = crud.get(session)
#         table_info = get_table_columns(session, db, table)["comment"]
#         project_data.rename(columns=table_info, inplace=True)
#     return project_data


# project_df = get_sales_project_table()

def layout():
    return html.Div(
    [
        project_overview.layout
    ]
)


# @app.callback(
#     Output("table-projects-overview", "data"),
#     Input("button-add-sale-project", "nClicks"),
#     State("table-projects-overview", "data"),
#     prevent_initial_call=True,
# )
# def table_editable_demo(nClicks, current_data):
#     """在表中新增空白行"""
#     if current_data:
#         return current_data.append({key: "" for key in project_df.columns})
#     else:
#         return [{key: "" for key in project_df.columns}]
